package com.xq.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.xq.domain.Menu;
import com.xq.domain.Order;
import com.xq.domain.Yuser;
import com.xq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@Controller
public class UserController {



    @Autowired
    UserService userService;
    
    
    @RequestMapping("deng")
    public String deng(HttpServletRequest rs){


        return "deng";
    }

    @RequestMapping("denglu")
    public String denglu(HttpServletRequest rs, Yuser yuser){


        Yuser log = userService.log(yuser);

        if (log!=null){

            rs.getSession().setAttribute("cun",log);

        }

        rs.setAttribute("huixian",yuser);

        return "redirect:list";

    }


    @RequestMapping("list")
    public String list(HttpServletRequest rs,Order order, @RequestParam(defaultValue = "1")int a){


        PageInfo<Order> fen = userService.fen(order, a, 3);
        for (Order order1 : fen.getList()) {
            System.err.println(order1);

        }


        rs.setAttribute("list",fen);


        return "list";
    }


    
    @RequestMapping("add")
    public String add(HttpServletRequest rs){

        Snowflake snowflake = IdUtil.getSnowflake(31, 31);
        long l = snowflake.nextId();
        Yuser cun =(Yuser) rs.getSession().getAttribute("cun");

        rs.setAttribute("xue",l);
        rs.setAttribute("cun",cun);
        return "add";

    }

    @RequestMapping("tian")

    public String tian(HttpServletRequest rs,Order order){


        System.err.println("-----"+order);

        int add = userService.add(order);



        return "redirect:list";
    }


    @RequestMapping("index")
    public String index(HttpServletRequest rs){

        Yuser cun =(Yuser) rs.getSession().getAttribute("cun");

        rs.setAttribute("xue",cun);

        ArrayList<Menu> ss = userService.ss();
        rs.setAttribute("ss",ss);


        return "index";
    }


}
